//
//  SHUtilitiesTool.m
//  youjian03
//
//  Created by Dadeso on 14-10-22.
//  Copyright (c) 2014年 com.shouhuobao. All rights reserved.
//

#import "SHUtilitiesTool.h"
#import "NSDate+MJ.h"

@implementation SHUtilitiesTool

+ (NSString*)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData  *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}


+ (NSDictionary *)dictionaryWithJsonString:(id)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    NSData *jsonData = jsonString;
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
       
        return nil;
    }
    return dic;
}

//获取UUID
+ (NSString*)UUIDString {
    CFUUIDRef uuidRef = CFUUIDCreate(kCFAllocatorDefault);
    CFStringRef stringRef = CFUUIDCreateString(kCFAllocatorDefault, uuidRef);
    CFRelease(uuidRef);
    return (__bridge NSString *)stringRef;
}

+ (UIColor *) colorWithHexString: (NSString *) stringToConvert
{
	NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
	
	// String should be 6 or 8 characters
	//if ([cString length] < 6) return DEFAULT_VOID_COLOR;
	
	// strip 0X if it appears
	if ([cString hasPrefix:@"0X"]) cString = [cString substringFromIndex:2];
	if ([cString hasPrefix:@"#"]) cString = [cString substringFromIndex:1];
	//if ([cString length] != 6) return DEFAULT_VOID_COLOR;
	// Separate into r, g, b substrings
	NSRange range;
	range.location = 0;
	range.length = 2;
	NSString *rString = [cString substringWithRange:range];
	
	range.location = 2;
	NSString *gString = [cString substringWithRange:range];
	
	range.location = 4;
	NSString *bString = [cString substringWithRange:range];
	
	// Scan values
	unsigned int r, g, b;
	[[NSScanner scannerWithString:rString] scanHexInt:&r];
	[[NSScanner scannerWithString:gString] scanHexInt:&g];
	[[NSScanner scannerWithString:bString] scanHexInt:&b];
	//NSLog(@"%d,%d,%d", r,g,b);
	
    //	return [UIColor colorWithRed:((float) r / 255.0f)
    //						   green:((float) g / 255.0f)
    //							blue:((float) b / 255.0f)
    //						   alpha:1.0f];
	return SF_COLOR(((float) r / 255.0f),((float) g / 255.0f),((float) b / 255.0f), 1);
}

+(NSString *) compareCurrentTime:(NSDate*) compareDate
//
{
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小前",temp];
    }
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    return  result;
}

+ (NSString *)numbersFromTodayByTime:(NSString *)time returnstyle:(NSString *)style
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
    NSDate *newDate = [formatter dateFromString:time];// 时分秒转为00:00:00
    NSDate *newDate2 = [formatter dateFromString:[formatter stringFromDate:newDate]];
    double timedif = [newDate2 timeIntervalSinceNow]*(-1);
    if ([style isEqualToString:@"sec"]) {
        return [NSString stringWithFormat:@"%d", (NSInteger)timedif];
    }else if ([style isEqualToString:@"min"]){
        return [NSString stringWithFormat:@"%d", (NSInteger)(timedif / 60)];
    }else if ([style isEqualToString:@"hour"]){
        return [NSString stringWithFormat:@"%d", (NSInteger)(timedif / (60*60))];
    }else if ([style isEqualToString:@"day"]){
        return [NSString stringWithFormat:@"%d", (NSInteger)(timedif / (24*60*60))];
    }else if ([style isEqualToString:@"mon"]){
        return [NSString stringWithFormat:@"%d", (NSInteger)(timedif / (24*60*60*30))];
    }else if ([style isEqualToString:@"year"]){
        return [NSString stringWithFormat:@"%d", (NSInteger)(timedif / (24*60*60*365))];
    }else if ([style isEqualToString:@"auto"]){
        if (timedif < 60) {
            if (timedif< 15) {
                return @"刚刚";
            }else{
                return [NSString stringWithFormat:@"%d 秒前",(NSInteger)timedif];
            }
        }else if (timedif > 60 && timedif < 3600){
            return [NSString stringWithFormat:@"%d 分钟前",(NSInteger)(timedif / 60)];
        }else if (timedif > 3600 && timedif < 3600*24){
            return [NSString stringWithFormat:@"%d 小时前",(NSInteger)(timedif / (60*60))];
        }else if (timedif > 3600*24 && timedif < 3600*24*30){
            return [NSString stringWithFormat:@"%d 天前",(NSInteger)(timedif / (24*60*60))];
        }else if (timedif > 3600*24*30 && timedif < 3600*24*365){
            return [NSString stringWithFormat:@"%d 月前",(NSInteger)(timedif / (24*60*60*30))];
        }else if (timedif > 3600*24*365){
            return [NSString stringWithFormat:@"%d 年前",(NSInteger)(timedif / (24*60*60*365))];
        }else{
            return [NSString stringWithFormat:@"%d 秒前",(NSInteger)timedif];
        }
    }else{
        return [NSString stringWithFormat:@"%d",(NSInteger)timedif];
    }
}

+ (NSString *)createdWithTime:(NSDate *)date
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"EEE MMM dd HH:mm:ss Z yyyy";
    
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
    
    // 获得微博发布的具体时间
    NSDate *createDate = date;
    
    // 判断是否为今年
    if (createDate.isThisYear) {
        if (createDate.isToday) { // 今天
            NSDateComponents *cmps = [createDate deltaWithNow];
            if (cmps.hour >= 1) { // 至少是1小时前发的
                return [NSString stringWithFormat:@"%d小时前", cmps.hour];
            } else if (cmps.minute >= 1) { // 1~59分钟之前发的
                return [NSString stringWithFormat:@"%d分钟前", cmps.minute];
            } else { // 1分钟内发的
                return @"刚刚";
            }
        } else if (createDate.isYesterday) { // 昨天
            fmt.dateFormat = @"昨天 HH:mm";
            return [fmt stringFromDate:createDate];
        } else { // 至少是前天
            fmt.dateFormat = @"MM-dd HH:mm";
            return [fmt stringFromDate:createDate];
        }
    } else { // 非今年
        fmt.dateFormat = @"yyyy-MM-dd";
        return [fmt stringFromDate:createDate];
    }
}


@end
